package cc.laop.java.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * @Auther: Pengpeng
 * @Date: create in 2018/1/11 18:16
 * @Description:
 */
public class CountDownLatchTest {

    public static CountDownLatch cdl;

    public static class Student extends Thread {
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread() + " -- finished !");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                cdl.countDown();
            }
        }
    }

    public static class Teacher {
        public void finnish() {
            try {
                cdl.await();
                System.out.println(Thread.currentThread() + " -- 考试结束，收卷");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        cdl = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Student stu = new Student();
            stu.start();
        }
        Teacher tch = new Teacher();
        tch.finnish();
    }

}
